#!/usr/bin/env bash

# C++ 项目管理脚本
# 用于在 cpp_box 工作空间中管理多个 C++ 项目

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 项目配置 - 使用简单数组而不是关联数组以提高兼容性
PROJECTS="cpp_grpc:gRPC聊天应用项目 cpp_basic:基础C++项目 cpp_opencv:OpenCV计算机视觉项目 cpp_qt:QtGUI应用项目"

# 获取项目描述
get_project_desc() {
    local project_name=$1
    for item in $PROJECTS; do
        if [[ $item == $project_name:* ]]; then
            echo "${item#*:}"
            return
        fi
    done
    echo "未知项目"
}

# 获取所有项目名称
get_all_projects() {
    for item in $PROJECTS; do
        echo "${item%:*}"
    done
}

# 显示帮助信息
show_help() {
    echo -e "${BLUE}C++ 项目管理器${NC}"
    echo "用法: $0 [命令] [项目名]"
    echo ""
    echo "命令:"
    echo "  list                    - 列出所有可用项目"
    echo "  create <project_name>   - 创建新项目"
    echo "  switch <project_name>   - 切换到指定项目"
    echo "  build <project_name>    - 构建指定项目"
    echo "  clean <project_name>    - 清理指定项目"
    echo "  status                  - 显示当前项目状态"
    echo "  help                    - 显示此帮助信息"
    echo ""
    echo "可用项目:"
    for project in $(get_all_projects); do
        echo "  - $project: $(get_project_desc $project)"
    done
}

# 列出所有项目
list_projects() {
    echo -e "${BLUE}可用的 C++ 项目:${NC}"
    for project in $(get_all_projects); do
        if [ -d "$project" ]; then
            echo -e "  ${GREEN}✓${NC} $project - $(get_project_desc $project)"
        else
            echo -e "  ${YELLOW}○${NC} $project - $(get_project_desc $project) (未创建)"
        fi
    done
}

# 创建新项目
create_project() {
    local project_name=$1
    
    if [ -z "$project_name" ]; then
        echo -e "${RED}错误: 请指定项目名称${NC}"
        return 1
    fi
    
    if [ -d "$project_name" ]; then
        echo -e "${YELLOW}项目 $project_name 已存在${NC}"
        return 1
    fi
    
    echo -e "${BLUE}创建项目: $project_name${NC}"
    
    # 创建项目目录结构
    mkdir -p "$project_name"/{src,include,build,doc}
    
    # 创建基本的 CMakeLists.txt
    cat > "$project_name/CMakeLists.txt" << EOF
cmake_minimum_required(VERSION 3.16)
project($project_name)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 包含目录
include_directories(include)

# 源文件
file(GLOB_RECURSE SOURCES "src/*.cpp")

# 可执行文件
add_executable(\${PROJECT_NAME} \${SOURCES})

# 编译选项
target_compile_options(\${PROJECT_NAME} PRIVATE -Wall -Wextra)
EOF
    
    # 创建基本的 Makefile
    cat > "$project_name/Makefile" << EOF
CXX = clang++
CXXFLAGS = -std=c++17 -Wall -Wextra -Iinclude
SRCDIR = src
BUILDDIR = build
SOURCES = \$(wildcard \$(SRCDIR)/*.cpp)
OBJECTS = \$(SOURCES:\$(SRCDIR)/%.cpp=\$(BUILDDIR)/%.o)
TARGET = \$(BUILDDIR)/$project_name

.PHONY: all clean

all: \$(TARGET)

\$(TARGET): \$(OBJECTS) | \$(BUILDDIR)
	\$(CXX) \$(OBJECTS) -o \$@

\$(BUILDDIR)/%.o: \$(SRCDIR)/%.cpp | \$(BUILDDIR)
	\$(CXX) \$(CXXFLAGS) -c \$< -o \$@

\$(BUILDDIR):
	mkdir -p \$(BUILDDIR)

clean:
	rm -rf \$(BUILDDIR)

run: \$(TARGET)
	./\$(TARGET)
EOF
    
    # 创建示例源文件
    cat > "$project_name/src/main.cpp" << EOF
#include <iostream>

int main() {
    std::cout << "Hello from $project_name!" << std::endl;
    return 0;
}
EOF
    
    # 创建 README
    cat > "$project_name/README.md" << EOF
# $project_name

$(get_project_desc $project_name)

## 构建

\`\`\`bash
make
\`\`\`

## 运行

\`\`\`bash
make run
\`\`\`

## 清理

\`\`\`bash
make clean
\`\`\`
EOF
    
    echo -e "${GREEN}项目 $project_name 创建成功!${NC}"
}

# 构建项目
build_project() {
    local project_name=$1
    
    if [ -z "$project_name" ]; then
        echo -e "${RED}错误: 请指定项目名称${NC}"
        return 1
    fi
    
    if [ ! -d "$project_name" ]; then
        echo -e "${RED}错误: 项目 $project_name 不存在${NC}"
        return 1
    fi
    
    echo -e "${BLUE}构建项目: $project_name${NC}"
    cd "$project_name"
    
    if [ -f "build.sh" ]; then
        ./build.sh
    elif [ -f "Makefile" ]; then
        make
    elif [ -f "CMakeLists.txt" ]; then
        mkdir -p build
        cd build
        cmake ..
        make
        cd ..
    else
        echo -e "${RED}错误: 找不到构建文件${NC}"
        return 1
    fi
    
    cd ..
    echo -e "${GREEN}项目 $project_name 构建完成!${NC}"
}

# 清理项目
clean_project() {
    local project_name=$1
    
    if [ -z "$project_name" ]; then
        echo -e "${RED}错误: 请指定项目名称${NC}"
        return 1
    fi
    
    if [ ! -d "$project_name" ]; then
        echo -e "${RED}错误: 项目 $project_name 不存在${NC}"
        return 1
    fi
    
    echo -e "${BLUE}清理项目: $project_name${NC}"
    cd "$project_name"
    
    if [ -f "Makefile" ]; then
        make clean
    else
        rm -rf build/*
    fi
    
    cd ..
    echo -e "${GREEN}项目 $project_name 清理完成!${NC}"
}

# 显示项目状态
show_status() {
    echo -e "${BLUE}项目状态:${NC}"
    
    for project in $(get_all_projects); do
        if [ -d "$project" ]; then
            echo -e "  ${GREEN}✓${NC} $project"
            if [ -d "$project/build" ] && [ "$(ls -A $project/build 2>/dev/null)" ]; then
                echo -e "    ${GREEN}已构建${NC}"
            else
                echo -e "    ${YELLOW}未构建${NC}"
            fi
        else
            echo -e "  ${RED}✗${NC} $project (未创建)"
        fi
    done
}

# 主函数
main() {
    case "${1:-help}" in
        "list")
            list_projects
            ;;
        "create")
            create_project "$2"
            ;;
        "build")
            build_project "$2"
            ;;
        "clean")
            clean_project "$2"
            ;;
        "status")
            show_status
            ;;
        "help"|*)
            show_help
            ;;
    esac
}

# 运行主函数
main "$@"